package qeug.shsh.facde.controller;

import com.drew.lang.annotations.NotNull;
import com.huawei.shade.org.apache.commons.logging.Log;
import com.huawei.shade.org.apache.commons.logging.LogFactory;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.openapi.common.custom.annotation.ApiController;
import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
import kd.bos.openapi.common.custom.annotation.ApiRequestBody;
import kd.bos.openapi.common.custom.annotation.ApiResponseBody;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.ORM;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import qeug.shsh.facde.info.IAMOrgModel;
import qeug.shsh.facde.info.IAMPositionModel;
import qeug.shsh.facde.resultext.UserResultExt;
import qeug.shsh.helper.FacadeHelper;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@ApiController(value = "IAMPositionController", desc = "金蝶IAM同步岗位接口，自定义插件")
public class IAMPositionController {

    private final static Log logger = LogFactory.getLog(IAMOrgController.class);//日志

    static CustomApiResult auditApiResultExt = new CustomApiResult();

    static UserResultExt userResultExt = new UserResultExt();

    @ApiPostMapping(value = "/shsh_iamPosition", desc = "组织许可api接口")
    public CustomApiResult<@ApiResponseBody("返回参数") UserResultExt> resultExt
            (@NotNull @ApiRequestBody(value = "入参") IAMPositionModel model) throws ParseException {

        String name = model.getName();//岗位名称
        String code = model.getCode();//岗位编码
        String orgCode = model.getOrgCode();//所在部门编码
        String status = model.getStatus();//状态
        String createTime = model.getCreateTime();//创建时间
        String updateTime = model.getUpdateTime();//更新时间
        String rankCode = model.getRankCode();//职级

        DynamicObject positionInfo = ORM.create().newDynamicObject("bos_position");
        positionInfo.set("number",code);//编码
        positionInfo.set("name",name);//名称
        DynamicObject orgInfo = FacadeHelper.getDynamicObject("bos_org", "number", orgCode);
        if (orgInfo==null){
            userResultExt.setMsg("人员接口调用失败，错误原因： 该岗位部门编号在系统中未找到！");
            userResultExt.setData(code);
            userResultExt.setTimestamp(new Timestamp(new Date().getTime()).toString());
            userResultExt.setCode("400");
            return CustomApiResult.success(userResultExt);
        }
        positionInfo.set("dpt",orgInfo);//部门
        if (status.equals("2")){
            positionInfo.set("enable","0");//状态
        }else{
            positionInfo.set("enable",status);//状态
        }
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        positionInfo.set("createtime",ft.parse(createTime));//创建时间
        positionInfo.set("modifytime",ft.parse(updateTime));//更新时间
        positionInfo.set("qeug_rank",rankCode);//职级
        positionInfo.set("status","C");//状态
        positionInfo.set("superiorposition",FacadeHelper.getDynamicObject("bos_position","number","PW-00000001"));//上级岗位



        DynamicObjectCollection dynamicObjects = new DynamicObjectCollection();
        dynamicObjects.add(positionInfo);
        OperationResult result = OperationServiceHelper.executeOperate("save", "bos_position", dynamicObjects.toArray(new DynamicObject[]{}), null);

        if (!result.isSuccess()) {
            userResultExt.setMsg("岗位接口调用失败，错误原因：" + result.getMessage());
            userResultExt.setData(code);
            userResultExt.setTimestamp(new Timestamp(new Date().getTime()).toString());
            userResultExt.setCode("400");
            return CustomApiResult.success(userResultExt);
        }else{
            userResultExt.setMsg("岗位接口调用成功！");
            userResultExt.setData(code);
            userResultExt.setTimestamp(new Timestamp(new Date().getTime()).toString());
            userResultExt.setCode("0");
            return CustomApiResult.success(userResultExt);
        }
    }
}
